package com.samsung.spen.sshare.explorer.choosefiles;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.samsung.spen.sshare.R;
import com.samsung.spen.sshare.explorer.util.SConfig;
import com.samsung.spen.sshare.explorer.util.FileSelected;

public class TestActivity extends ListActivity {

	private TextView textView;
	private static int SLEEP_THREAD = 2000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exp_activity_main);

		textView = (TextView) findViewById(R.id.tvNumber);
	}

	@Override
	protected void onResume() {
		super.onResume();

		BroadcastReceiver broadcastReceiver = new MyBroadcast();
		IntentFilter filter = new IntentFilter(SConfig.INTENT_DATA);

		registerReceiver(broadcastReceiver, filter);
	}

	public void onclick(View v) {

		Intent intent = new Intent(this, FilesChooseActivity.class);
		startActivity(intent);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {

			System.exit(0);
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private ArrayList<String> arrayList;
	private ArrayAdapter<String> adapter;
	private String[] strings;

	public class MyBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {

			try {

				// arrayList = intent
				// .getStringArrayListExtra(Config.PATH_LIST_FILES);
				//
				// if (arrayList != null && arrayList.size() > 0) {
				//
				// String[] strings = new String[arrayList.size()];
				//
				// for (int i = 0; i < strings.length; i++) {
				//
				// FileSelected file = new FileSelected(arrayList.get(i));
				// strings[i] = file.toString();
				// }
				//
				// adapter = new ArrayAdapter<String>(MainActivity.this,
				// android.R.layout.simple_list_item_1, strings);
				// setListAdapter(adapter);
				//
				// textView.setText("List of files to send: " + strings.length
				// + " files");
				//
				// ((Button) findViewById(R.id.btnChooseFiles))
				// .setText("Change files selected");
				//
				// } else {
				//
				// Builder builder = new Builder(MainActivity.this);
				// builder.setMessage("No data");
				// }

				new LoadFiles().execute(intent);

			} catch (Exception e) {
				Log.e("Loi o day==>", "ham nhan receiver", e);
			}

		}

	}

	private class LoadFiles extends AsyncTask<Intent, Void, Void> {

		ProgressDialog dialog;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = ProgressDialog.show(TestActivity.this, "", "loading...");
		}

		@Override
		protected Void doInBackground(Intent... params) {

			Intent intent = params[0];
			arrayList = intent.getStringArrayListExtra(SConfig.PATH_LIST_FILES);

			if (arrayList != null && arrayList.size() > 0) {

				strings = new String[arrayList.size()];

				for (int i = 0; i < strings.length; i++) {

					FileSelected file = new FileSelected(arrayList.get(i));
					strings[i] = file.toString();
				}

				adapter = new ArrayAdapter<String>(TestActivity.this,
						android.R.layout.simple_list_item_1, strings);
				// setListAdapter(adapter);

				// textView.setText("List of files to send: " + strings.length
				// + " files");
				//
				// ((Button) findViewById(R.id.btnChooseFiles))
				// .setText("Change files selected");
				try {
					Thread.sleep(SLEEP_THREAD);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {

				// Builder builder = new Builder(MainActivity.this);
				// builder.setMessage("No data");
				return null;
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);

			setListAdapter(adapter);

			textView.setText("List of files to send: " + strings.length
					+ " files");

			((Button) findViewById(R.id.btnChooseFiles))
					.setText("Change files selected");

			SLEEP_THREAD = 200;
			dialog.dismiss();
		}

	}
}
